package com.wxj.book.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxj.book.cms.dao.EducationMapper;
import com.wxj.book.cms.pojo.Education;
import com.wxj.book.cms.service.EducationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EducationServiceImpl implements EducationService {
    @Autowired
    private EducationMapper educationMapper;

    /**
     * 带有分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Education> getAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Education> list = educationMapper.getAll();
        PageInfo<Education> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 更新或者添加学年制
     * @param education
     * @return
     */
    @Override
    public int updateEducation(Education education) {
        if (education.getId() == null) {
            Education newEducation = new Education();
            newEducation.setCode(education.getCode());
            newEducation.setName(education.getName());
            newEducation.setStatus(education.getStatus());
            return educationMapper.insertSelective(newEducation);
        }
        Education newEducation = new Education();
        newEducation.setId(education.getId());
        newEducation.setCode(education.getCode());
        newEducation.setName(education.getName());
        newEducation.setStatus(education.getStatus());
        return educationMapper.updateByPrimaryKeySelective(newEducation);
    }

    /**
     * 根据id删除
     * @param educationId
     * @return
     */
    @Override
    public int deleteById(Integer educationId) {
        return educationMapper.deleteByPrimaryKey(educationId);
    }
}
